﻿using System;

namespace Lr.Templating.FluentMapping
{
    /// <summary>
    /// Implementation of <see cref="INonGenericTemplateMapping"/>,
    /// which defines a mapping between a template's placeholder and the value to generate for the placeholder.
    /// </summary>
    internal class NonGenericTemplateMapping : TemplateMappingBase, INonGenericTemplateMapping
    {
        /// <summary>
        /// Constructs a new <see cref="NonGenericTemplateMapping"/> instance.
        /// </summary>
        /// <param name="placeholder">The placeholder string.</param>
        /// <param name="value">The string value that will act as a replacement for the placeholder.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="placeholder"/> is null, empty or only exists of white spaces.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="value"/> is null, empty or only exists of white spaces.</exception>
        internal NonGenericTemplateMapping(string placeholder, string value)
            : base(placeholder)
        {
            ThrowIf.NullOrEmpty(() => value);

            _value = () => value;
        }

        /// <summary>
        /// Constructs a new <see cref="NonGenericTemplateMapping"/> instance.
        /// </summary>
        /// <param name="placeholder">The placeholder string.</param>
        /// <param name="value">The function, that returns a string, that generates the value that will act as a replacement for the placeholder.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="placeholder"/> is null, empty or only exists of white spaces.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="value"/> is null.</exception>
        internal NonGenericTemplateMapping(string placeholder, Func<string> value)
            : base(placeholder)
        {
            ThrowIf.Null(() => value);

            _value = value;
        }

        /// <summary>
        /// Function that generates the value that will act as a replacement for the placeholder.
        /// </summary>
        protected Func<string> Value { get { return _value; } }
        private readonly Func<string> _value;

        /// <summary>
        /// Calculates, and returns the value for the placeholder.
        /// </summary>
        /// <returns>The value in the form of a string.</returns>
        public string GetValue()
        {
            return this.Value();
        }
    }
}
